<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body></body>
</html>

<script>
  const canvas = document.createElement("canvas");
  canvas.width = 600;
  canvas.height = 400;
  document.body.append(canvas);
  const context = canvas.getContext("2d");

  // partten 铺满，填充的意思。像ps
  let img = new Image();
  img.src =
    "https://fastly.picsum.photos/id/225/100/100.jpg?hmac=RmNMOvt-ZNRl9OARBcNVr8-ceDZv_eNpshUn0WFIOvw";
  img.onload = function () {
    // 创建重复元素图像对象, 第二个参数可以选择 水平，垂直以及repeat,他就会一直重复了
    let p = context.createPattern(img, "repeat");
    context.fillStyle = p;

    context.fillRect(0, 0, 200, 200);
  };
</script>
